package com.mygdx.game.Screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.B2WorldCreator;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.Scenes.ActiveCharacter;
import com.mygdx.game.Scenes.Hud;
import com.mygdx.game.Sprite.Mario;
import com.mygdx.game.Stage.DialogueStage;


public class Boss2Screen implements Screen {
    public static MyGdxGame game;
    public static int positionFlag = 0;
    public static int stageFlag = 0;
    private OrthographicCamera gamecam;
    private Viewport gamePort;
    public static Mario mario;
    private Stage actor;
    private ActiveCharacter activeCharacter;
    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;
    //Box2d
    private World world;
    private Box2DDebugRenderer b2dr;

    public Boss2Screen(final MyGdxGame game) {
        this.game = game;


        //activeCharacter = new ActiveCharacter("Character/Protagonist",MyGdxGame.V_WIDTH/2-50,MyGdxGame.V_HEIGHT/2-50);


        gamecam = new OrthographicCamera();
        gamePort = new FitViewport(MyGdxGame.V_WIDTH/3 , MyGdxGame.V_HEIGHT/3 , gamecam);
        actor = new Stage();
      //  actor.setViewport(gamePort);
        mapLoader = new TmxMapLoader();
        map = mapLoader.load("Boss2.tmx");
        renderer = new OrthogonalTiledMapRenderer(map);
        gamecam.position.set(gamePort.getScreenWidth()/2,gamePort.getScreenHeight()/2,0);
        world = new World(new Vector2(0, 0), true);


        b2dr = new Box2DDebugRenderer();
        new B2WorldCreator(world,map,game);
        if (positionFlag==0) {
            mario = new Mario(world, 5, 618, 818);
        }
        if (positionFlag==1){
            mario = new Mario(world, 5, 580, 420);
        }
       // actor.addActor(activeCharacter);
        listen();
//        mario.b2Body.setUserData(dialogueStage.getActors());




    }
    public void listen(){
        world.setContactListener(new ContactListener() {
            @Override
            public void beginContact(Contact contact) {

                System.out.println(gamecam.position);
                if(gamecam.position.x>557 && gamecam.position.x<619 && gamecam.position.y>773 && gamecam.position.y<790)
                {
                    Boss1Screen.positionFlag=1;
                    game.setScreen(new Boss1Screen(game));
                    // dispose();

                }
                if(gamecam.position.x>550 && gamecam.position.x<625 && gamecam.position.y>440 && gamecam.position.y<460)
                {
                    game.setScreen(new Boss3Screen(game));
                }

            }

            @Override
            public void endContact(Contact contact) {

            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {

            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {

            }

        });
    }

    public void update(float dt) {
        mario.handleInput(dt);
        mario.update(dt);
        world.step(1 / 60f, 6, 2);
        //摄像头跟随人物底层图形
        gamecam.position.x = mario.b2Body.getPosition().x;
        gamecam.position.y = mario.b2Body.getPosition().y;
        gamecam.update();
        renderer.setView(gamecam);
    }

    @Override
    public void show() {

    }
    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        renderer.render();
        b2dr.render(world, gamecam.combined);
        //actor.draw();
        //actor.act();
        game.batch.setProjectionMatrix(gamecam.combined);
        game.batch.begin();
        mario.draw(game.batch);
        game.batch.end();
        GoHome();

        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            System.out.println("exit");
            Gdx.app.exit();
        }


    }
    @Override
    public void resize(int width, int height) {
        gamePort.update(width, height);
    }
    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        map.dispose();
        renderer.dispose();
        world.dispose();
        b2dr.dispose();


    }
    public void GoHome(){
        if (Gdx.input.isKeyPressed(Input.Keys.B)) {
            game.setScreen(new HomeScreen(game));
        }
    }
}
